programming4us
           
 
 
Windows Phone

Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Rendering Text

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/1/2010 5:45:57 PM
Another important reason to use the sprite batch is to render text to the screen. Notice a DrawString method on the sprite batch object, which is the method you use to draw the text. Before you get there, you need to do a few extra steps.

First, add a new item to your content project by right-clicking the project and selecting Add->New Item. In the dialog box, select the option to add a Sprite Font, name it Game, and click the Add button. Visual Studio opens this file, which is simply a standard XML file.

The first node of note is the FontName node, which (as expected) is the name of the font. The default here is Kootenay although we personally like Comic Sans MS. Select a font (browse through your fonts folder), and type the font name in the node. The next node is Size, which is how large you want the font to be. The default here is 14, but you can choose any size you’d like. The size roughly matches what you see if you change the font size in a text editor such as notepad. The sample with the downloadable examples is a size of 48.

Next is Spacing, which enables you to control how the letters are laid out when they draw. The default value is zero, so no extra spacing is placed between characters as they draw, but changing the value adds extra space (in pixels) between characters. It’s even possible to use a negative number to remove space between characters. Hand in hand with spacing, the next node is called UseKerning and controls whether kerning is used when rendering the text. Kerning changes how the spacing between characters is laid out. Rather than a fixed amount of space between each character, the amount of space depends on the character that comes before and the character that comes after. The default is true because in most cases it just looks better that way.

Next is the Style node, which enables you to choose what the style of the font. The default is Regular, but you can also choose Bold, Italic, or if you are adventurous, choose Bold, Italic. Note that these styles are case sensitive.

The last set of nodes controls which characters you expect to draw. The default includes all English characters (ASCII codes 32 through 126). To draw characters beyond this range, you can update the values here.

Now it’s time to get some text on the screen. First, add a new variable to the class to hold the font data:

SpriteFont font;

Then, load the new font in your LoadContent method:

font = Content.Load<SpriteFont>("Game");

Finally, replace your Draw method with the following to see how easy it is to add text rendering to your games:

GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(font,"XNA Game Studio 4.0", Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);

Everything except the DrawString should be quite familiar by now. There are a number of DrawString overloads, many of which mirror ones you saw earlier for rendering images rather than text. The DrawString methods do not take a Texture2D, but instead take a SpriteFont to get the font data. They also include an extra parameter that is either a string or a StringBuilder that is the text you want to render. They do not, however, include a destination rectangle or a source rectangle (because these do not make sense when rendering text). All other parameters to Draw exist in the overloads of DrawString, however, so you can freely scale, rotate, color, and layer your text rendering.

The sprite font object has a few methods and properties that are somewhat interesting. For example, what if you wanted to draw a second line of text just below the line you’ve already drawn? How would you know where to begin? Add the following lines of codes to your Draw method directly below the DrawString call:

Vector2 stringSize = font.MeasureString("XNA Game Studio 4.0");
spriteBatch.DrawString(font, "rocks!", new Vector2(0,stringSize.Y), Color.White);

Here, you use the MeasureString method on the sprite font object, which calculates the size (in pixels) that the rendered string takes up. You can then use that size to start the next line of text rendered down by an appropriate number of pixels. Running the application now shows you two lines of text as in Figure 1.
Figure 1. Text rendering


Other -----------------
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 3)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 2)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 1)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Show Me Something on Screen
- Windows Phone 7 : Working with SharePoint Documents
- Windows Phone 7 : Connecting to SharePoint
- Windows Phone 7 : Synching Notes to the Web
- Windows Phone 7 : Using OneNote Mobile
- Windows Phone 7 : Using PowerPoint Mobile
- Windows Phone 7 : Using Excel Mobile
- Windows Phone 7 : Using Word Mobile
- Windows Phone 7 : Saving and Deleting Documents
- Windows Phone 7 : Sharing Documents via E-Mail
- Windows Phone 7 : Opening Documents
- Windows Phone 7 : Managing Storage on Your Phone
- Windows Phone 7 : Changing Zune Sync Settings
- Windows Phone 7 : Synching with Your PC - Adding Media to Your Zune Collection
- Windows Phone 7 : Synching Files Wirelessly
- Windows Phone 7 : Synching with Your PC - Seeing What’s Synching
- Windows Phone 7 : Synching Media with Your Phone
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us